#!/usr/bin/env python
# -*- coding: utf8 -*-
# title       :
# description :
# author      :'ShenMeng'
# coding:utf-8

from PySide import QtGui
from PySide import QtCore
import sys
import math


class NoBorderDialog(QtGui.QDialog):
    def __init__(self, parent=None):
        super(NoBorderDialog, self).__init__(parent)
        self._padding = 5
        self.SHADOW_WIDTH=0   #边框距
        self.isLeftPressDown = False #鼠标左键是否按下
        self.dragPosition=0     #拖动时坐标
        self.Numbers = self.enum(UP=0, DOWN=1, LEFT=2, RIGHT=3, LEFTTOP=4, LEFTBOTTOM=5, RIGHTBOTTOM=6, RIGHTTOP=7, NONE=8) #枚举参数
        self.dir=self.Numbers.NONE #初始鼠标状态
        self.setWindowFlags(QtCore.Qt.Dialog | QtCore.Qt.FramelessWindowHint | QtCore.Qt.WindowSystemMenuHint)
        #self.setWindowFlags(QtCore.Qt.FramelessWindowHint | QtCore.Qt.WindowSystemMenuHint)
        self.setAttribute(QtCore.Qt.WA_TranslucentBackground)
        self.setMouseTracking(True)


    # 枚举参数
    def enum(self, **enums):
        return type('Enum', (), enums)

    # def region(self,cursorGlobalPoint):
    #     # 获取窗体在屏幕上的位置区域，tl为topleft点，rb为rightbottom点
    #     rect = self.rect()
    #     tl = self.mapToGlobal(rect.topLeft())
    #     rb = self.mapToGlobal(rect.bottomRight())
    #     x = cursorGlobalPoint.x()
    #     y = cursorGlobalPoint.y()
    #     if(tl.x() + self._padding >= x and tl.x() <= x and tl.y() + self._padding >= y and tl.y() <= y):
    #         #左上角
    #         self.dir = self.Numbers.LEFTTOP
    #         # 设置鼠标形状
    #         self.setCursor(QtGui.QCursor(QtCore.Qt.SizeFDiagCursor))
    #     elif(x >= rb.x() - self._padding and x <= rb.x() and y >= rb.y() - self._padding and y <= rb.y()):
    #         #右下角
    #         self.dir = self.Numbers.RIGHTBOTTOM
    #         self.setCursor(QtGui.QCursor(QtCore.Qt.SizeFDiagCursor))
    #     elif(x <= tl.x() + self._padding and x >= tl.x() and y >= rb.y() - self._padding and y <= rb.y()):
    #         # 左下角
    #         self.dir = self.Numbers.LEFTBOTTOM
    #         self.setCursor(QtGui.QCursor(QtCore.Qt.SizeBDiagCursor))
    #     elif(x <= rb.x() and x >= rb.x() - self._padding and y >= tl.y() and y <= tl.y() + self._padding):
    #         #右上角
    #         self.dir = self.Numbers.RIGHTTOP
    #         self.setCursor(QtGui.QCursor(QtCore.Qt.SizeBDiagCursor))
    #     elif(x <= tl.x() + self._padding and x >= tl.x()):
    #         #左边
    #         self.dir = self.Numbers.LEFT
    #         self.setCursor(QtGui.QCursor(QtCore.Qt.SizeHorCursor))
    #     elif( x <= rb.x() and x >= rb.x() - self._padding):
    #         #右边
    #         self.dir = self.Numbers.RIGHT
    #         self.setCursor(QtGui.QCursor(QtCore.Qt.SizeHorCursor))
    #     elif(y >= tl.y() and y <= tl.y() + self._padding):
    #         #上边
    #         self.dir = self.Numbers.UP
    #         self.setCursor(QtGui.QCursor(QtCore.Qt.SizeVerCursor))
    #     elif(y <= rb.y() and y >= rb.y() - self._padding):
    #         #下边
    #         self.dir = self.Numbers.DOWN
    #         self.setCursor(QtGui.QCursor(QtCore.Qt.SizeVerCursor))
    #     else:
    #         #默认
    #         self.dir = self.Numbers.NONE
    #         self.setCursor(QtGui.QCursor(QtCore.Qt.ArrowCursor))
    #
    # def mouseReleaseEvent(self,event):
    #
    #     if(event.button() == QtCore.Qt.LeftButton):
    #         self.isLeftPressDown = False
    #     if(self.dir != self.Numbers.NONE):
    #         self.releaseMouse()
    #         self.setCursor(QtGui.QCursor(QtCore.Qt.ArrowCursor))
    #
    # def mousePressEvent(self,event):
    #     if(event.button()==QtCore.Qt.LeftButton):
    #         self.isLeftPressDown=True
    #     if(self.dir != self.Numbers.NONE):
    #         self.mouseGrabber()
    #     else:
    #
    #         self.dragPosition = event.globalPos() - self.frameGeometry().topLeft()
    #
    # def mouseMoveEvent(self,event):
    #     gloPoint = event.globalPos()
    #     rect = self.rect()
    #     tl = self.mapToGlobal(rect.topLeft())
    #     rb = self.mapToGlobal(rect.bottomRight())
    #
    #     if(not self.isLeftPressDown):
    #         self.region(gloPoint)
    #     else:
    #         if(self.dir != self.Numbers.NONE):
    #             rmove = QtCore.QRect(tl, rb)
    #             if(self.dir==self.Numbers.LEFT):
    #                 if(rb.x() - gloPoint.x() <= self.minimumWidth()):
    #                     rmove.setX(tl.x())
    #                 else:
    #                     rmove.setX(gloPoint.x())
    #             elif(self.dir==self.Numbers.RIGHT):
    #                 rmove.setWidth(gloPoint.x() - tl.x())
    #             elif(self.dir==self.Numbers.UP):
    #                 if(rb.y() - gloPoint.y() <= self.minimumHeight()):
    #                     rmove.setY(tl.y())
    #                 else:
    #                     rmove.setY(gloPoint.y())
    #             elif(self.dir==self.Numbers.DOWN):
    #                 rmove.setHeight(gloPoint.y() - tl.y())
    #             elif(self.dir==self.Numbers.LEFTTOP):
    #                 if(rb.x() - gloPoint.x() <= self.minimumWidth()):
    #                     rmove.setX(tl.x())
    #                 else:
    #                     rmove.setX(gloPoint.x())
    #                 if(rb.y() - gloPoint.y() <= self.minimumHeight()):
    #                     rmove.setY(tl.y())
    #                 else:
    #                     rmove.setY(gloPoint.y())
    #             elif(self.dir==self.Numbers.RIGHTTOP):
    #                 rmove.setWidth(gloPoint.x() - tl.x())
    #                 rmove.setY(gloPoint.y())
    #             elif(self.dir==self.Numbers.LEFTBOTTOM):
    #                 rmove.setX(gloPoint.x())
    #                 rmove.setHeight(gloPoint.y() - tl.y())
    #             elif(self.dir==self.Numbers.RIGHTBOTTOM):
    #                 rmove.setWidth(gloPoint.x() - tl.x())
    #                 rmove.setHeight(gloPoint.y() - tl.y())
    #             else:
    #                 pass
    #             self.setGeometry(rmove)
    #         else:
    #             return
    #             #super(NoBorderDialog, self).mouseMoveEvent(event)
    #             #self.move(event.globalPos() - self.dragPosition)
    #         event.accept()


    def eventFilter(self, source, event):
        if event.type() == QtCore.QEvent.MouseMove:
            if event.buttons() == QtCore.Qt.NoButton:
                gloPoint = event.globalPos()
                self.region(gloPoint)
            else:
                pass  # do other stuff
        return super(NoBorderDialog, self).eventFilter(source, event)


    def paintEvent(self, event):
        # ###---2---###
        path = QtGui.QPainterPath()
        path.setFillRule(QtCore.Qt.WindingFill)
        path.addRect(5, 5, self.width() - 10, self.height() - 10)

        painter = QtGui.QPainter(self)
        painter.setRenderHint(QtGui.QPainter.Antialiasing, True)
        painter.fillPath(path, QtGui.QBrush(QtGui.QColor(0, 170, 230)))
        color = QtGui.QColor(0, 0, 0, 50)
        for i in range(5):
            path = QtGui.QPainterPath()
            path.setFillRule(QtCore.Qt.WindingFill)
            path.addRect(5 - i, 5 - i, self.width() - (5 - i) * 2, self.height() - (5 - i) * 2)
            color.setAlpha(100 - math.sqrt(i) * 50)
            painter.setPen(color)
            painter.drawPath(path)

if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    shadow = NoBorderDialog()
    shadow.show()
    app.exec_()